#include "../include/Application.hpp"
#include "../include/BackgroundManager.hpp"
#include "../include/InputEventManager.hpp"
#include "../include/Game.hpp"
#include "../include/SpritesManager.hpp"
#include "../build/BG_BOTTOM_DAY_1_1.h"
#include "../build/BG_BOTTOM_DAY_1_2.h"
#include "../build/BG_TOP_DAY_DAY_1.h"
#include "../build/LOGO_RITH.h"
#include "../build/PRESS_START.h"
#include "stdio.h"
#include <maxmod9.h>
#include "soundbank.h"
#include "soundbank_bin.h"

Application::Application(void)
{
	bgManager = NULL;
	inputManager = NULL;
	state = NONE;
	soundMenu = false;
	soundGame = false;
	soundPause = false;
}


Application::~Application(void)
{
}

void Application::init(){
	loadSounds();
	
	bgManager = new BackgroundManager();
	inputManager = new InputEventManager();
	game = new Game(bgManager);
	initVideo();

	state = SPLASH;
		
}

void Application::update(){

	if(state != bgManager->loadedBG) bgManager->displayBackgrounds(state, this);
	inputManager->update();
	inputManager->processInputs(this);


	if(state == GAME ){
		
		game->update(this);
	}
	
	if (state == MENU && soundMenu == false) {
		mmStop();
		mmStart( MOD_MENU, MM_PLAY_LOOP );
		soundMenu = true;
		soundGame = false;
	}
	if (state == GAME && soundGame == false) { 
		if (soundMenu == true) {  
			mmStop(); 
			soundMenu = false;
		}
		if (soundPause == false) { 
			mmStart( MOD_GAME, MM_PLAY_LOOP );
		}

		if (soundPause == true) { 
			mmResume(); 
			soundPause = false; 
		}

		
		soundGame = true;
	}
	if (state == PAUSE && soundGame == true) {
		mmPause();
		soundGame = false; 
		soundPause = true;
	}
	swiWaitForVBlank();
	
}
void Application::loadSounds() {
	// load sounds + FX
	mmInitDefaultMem( (mm_addr)soundbank_bin );
	mmLoad(MOD_MENU);
	mmLoad(MOD_GAME);
	mmLoadEffect( SFX_HIT_2 );
	mmLoadEffect( SFX_HIT_GORE );
	mmLoadEffect( SFX_MANA );
	mmEffectVolume(mmEffect(SFX_MANA),255);
	mmEffectVolume(mmEffect(SFX_HIT_2),220);
		mmEffectVolume(mmEffect(SFX_HIT_GORE),220);
	mmSetModuleVolume(220);
	
}
void Application::initVideo(){

	lcdMainOnBottom();

    vramSetMainBanks(VRAM_A_MAIN_BG_0x06000000,
                     VRAM_B_MAIN_BG_0x06020000,
                     VRAM_C_SUB_BG_0x06200000,
                     VRAM_D_SUB_SPRITE);

    vramSetBankE(VRAM_E_MAIN_SPRITE);

    /*  Set the video mode on the main screen. */
    videoSetMode(MODE_5_2D | // Set the graphics mode to Mode 5
			        
				DISPLAY_BG2_ACTIVE | // Enable BG2 for display
                DISPLAY_BG3_ACTIVE | // Enable BG3 for display
                 DISPLAY_SPR_ACTIVE | // Enable sprites for display
                 DISPLAY_SPR_1D       // Enable 1D tiled sprites
                 );

    /*  Set the video mode on the sub screen. */
    videoSetModeSub(MODE_5_2D |
					DISPLAY_BG2_ACTIVE |// Set the graphics mode to Mode 5
                    DISPLAY_BG3_ACTIVE |
					DISPLAY_SPR_ACTIVE | // Enable sprites for display
					DISPLAY_SPR_1D );      // Enable 1D tiled sprites); // Enable BG3 for display
}


